/*
 * Copyright 2014 Cungmua, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cungmua.vn.kiosk.adapter;

import java.util.ArrayList;
import java.util.List;

import com.androidquery.AQuery;

import cungmua.vn.kiosk.R;
import cungmua.vn.kiosk.model.DonHang;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @author kelvin.nguyen
 * 
 */
public class DonHangAdapter extends BaseAdapter {
	Activity mContext;
	private List<DonHang> donhangs = new ArrayList<DonHang>();
	private AQuery aq;
	private DonHang donhang;

	public DonHangAdapter(Activity c, List<DonHang> donhangs) {
		mContext = c;
		this.donhangs = donhangs;
		aq = new AQuery(c);

	}

	@Override
	public int getCount() {
		int size = (donhangs != null) ? donhangs.size() : 0;
		return size;
	}

	@Override
	public Object getItem(int position) {
		DonHang item = (donhangs != null) ? donhangs.get(position) : null;
		return item;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void addItem(List<DonHang> items) {
		int size = items.size();
		for (int i = 0; i < size; i++) {
			DonHang item = items.get(i);
			donhangs.add(item);
		}
		this.notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		donhang = donhangs.get(position);

		LayoutInflater inflator = mContext.getLayoutInflater();
		if (convertView == null) {
			holder = new ViewHolder();

			convertView = inflator.inflate(R.layout.row_donhang, null);
			holder.txt_address = (TextView) convertView
					.findViewById(R.id.txt_address);

			holder.iv_type = (ImageView) convertView.findViewById(R.id.iv_type);
			holder.iv_vip = (ImageView) convertView.findViewById(R.id.iv_vip);
			holder.iv_restore = (ImageView) convertView
					.findViewById(R.id.iv_restore);
			holder.txt_name = (TextView) convertView
					.findViewById(R.id.txt_name);
			holder.txt_phone = (TextView) convertView
					.findViewById(R.id.txt_phone);
			holder.txt_ma_dh_building_name = (TextView) convertView
					.findViewById(R.id.txt_ma_dh_building_name);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.txt_name.setText(donhang.getCustomerName());
		holder.txt_address.setText(donhang.getAddress());
		holder.txt_phone.setText(String.valueOf(donhang.getPhone()));
		String buildingName = donhang.getBuildingName() != null ? donhang
				.getBuildingName() : "";

		String comment = donhang.getNote() != null ? ", " + donhang.getNote()
				: "";
		holder.txt_ma_dh_building_name.setText(mContext.getResources()
				.getString(R.string.label_DH)
				+ donhang.getOrderId()
				+ " "
				+ buildingName + comment);
		// holder.txt_comment.setText(donhang.getNote());

		if (donhang.getAddressType() == 0) {
			holder.iv_type.setBackgroundResource(R.drawable.icon_home);
		} else {
			holder.iv_type.setBackgroundResource(R.drawable.icon_office);
		}
		// check customer isVip ?
		int isShow = donhang.getIsVip() == 0 ? View.GONE : View.VISIBLE;
		holder.iv_vip.setVisibility(isShow);

		// check show/hide restore icon
		int isShowRestore = donhang.getNumberDelay() > 0 ? View.VISIBLE
				: View.GONE;
		holder.iv_restore.setVisibility(isShowRestore);

		return convertView;
	}

	public static class ViewHolder {
		TextView txt_address;
		ImageView iv_type;
		ImageView iv_vip;
		ImageView iv_restore;
		TextView txt_name;
		TextView txt_phone;
		TextView txt_ma_dh_building_name;

	}

}
